package guanqu.server;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import guanqu.beans.PhotoPoints;
import guanqu.models.PointXY;
import manager.PersistentManager;
import manager.Utils;




/**
 * 
 * Servlet implementation class ReceiveServlet
 */
//@WebServlet("/ReceiveServlet")
@MultipartConfig     //一定要加这一句，否则无法获取参数
public class ReceiveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReceiveServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String path = "e:/IrrigationAutomation/gis/photoPoints/";
	
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		
//		String data = request.getParameter("points");
//		ArrayList<PointXY> pointXYs = (ArrayList<PointXY>) JSON.parseArray(data, PointXY.class);
		
		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data,Upload.class);
		
		
		String fileName = request.getParameter("fileName");
		System.out.println("fileName:"+fileName);
		String fileType =request.getParameter("type");
		System.out.println("fileType:"+fileType);
		
		Part part = request.getPart("file");
		System.out.println("part:"+path);
		int len = (int)part.getSize();
		System.out.println("len:"+len);
		
		Calendar now_time = Calendar.getInstance();
		fileName = Utils.timestamp2Format2(now_time.getTimeInMillis());
		File file = new File(path+fileName+"."+fileType);
		InputStream is = part.getInputStream();
		FileOutputStream fos = new FileOutputStream(file);
		System.out.println("准备接收");
		byte[] buf = new byte[len];
		is.read(buf);
		fos.write(buf);
		is.close();
		fos.close();
		System.out.println("接收结束");
		
		// 保存至数据库
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
		Session session = sf.getCurrentSession();

		Transaction tx = session.beginTransaction();
		try
		{
			for(int i = 0;i< upload.getPoints().size();i++)
			{
				PointXY pointXY = upload.getPoints().get(i);
				PhotoPoints photoPoints = new PhotoPoints();
				photoPoints.setJingdu(pointXY.getX());
				photoPoints.setWeidu(pointXY.getY());
				photoPoints.setMeasureNum(i+"");
				photoPoints.setPhotoUri(fileName+"."+fileType);
				photoPoints.setRealName(upload.getUserName());
				photoPoints.setUserAccount(upload.getUserAccount());
				photoPoints.setTime(now_time);
				photoPoints.setRemark(upload.getRemark());
				session.save(photoPoints);
			}
			tx.commit();
		}catch(Exception e)
		{
			tx.rollback();
			e.printStackTrace();
		}finally
		{
//			session.close();
		}

	}
	
	// 上传数据
	public static class Upload
	{
		String userAccount; 	// 帐号
		String userName;		// 真名
		
		ArrayList<PointXY> points = new ArrayList<PointXY>();	// 测量信息
		String remark;		// 备注
		public String getUserAccount() {
			return userAccount;
		}
		public void setUserAccount(String userAccount) {
			this.userAccount = userAccount;
		}
		public String getUserName() {
			return userName;
		}
		public void setUserName(String userName) {
			this.userName = userName;
		}
		public ArrayList<PointXY> getPoints() {
			return points;
		}
		public void setPoints(ArrayList<PointXY> points) {
			this.points = points;
		}
		public String getRemark() {
			return remark;
		}
		public void setRemark(String remark) {
			this.remark = remark;
		}
		
		
		
		
	}

}

